<template>
	<view class="home">
		<custom-navbar title="我的供需" :show-back="true" @back="handleBack" />

		<view class="content" :style="{ paddingTop: navBarHeight + 'px' }">
			<view class="search mb37 p0030">
				<view class="flex">
					<view v-for='(item,index) in supplyDemandTypes' :key="index" class="f30bc3"
						:style="{ marginRight: index < supplyDemandTypes.length - 1 ? '67rpx' : '0' }"
						:class="supply_demand == item.value ? 'c398DFB' : ''"
						@click="switchSupplyDemandType(item.value)">
						{{item.label}}
					</view>
				</view>
			</view>

			<!-- 空状态 -->
			<view v-if="isEmpty" class="empty-state fcolcc">
				<image class="empty-image" src="/static/images/common/empty.png"></image>
				<view class="empty-text">暂无供需数据</view>
				<up-button type="primary" shape="circle" @click="resetSearch" style="width: 200rpx;margin-top: 30rpx;">
					重新加载
				</up-button>
			</view>

			<!-- 供需列表 -->
			<view v-else class="supplyList frw p0030" style="gap: 22rpx;">
				<view class="item" @click="handleDetails(item)" v-for="(item,index) in supplyDemandList" :key="index">
					<image class="img" :src="item.image" mode="aspectFill"></image>
					<view class="contentBox fcols">
						<view>
							<view class="fc">
								<image class="gong" v-if="item.supply_demand == 19" src="/static/images/home/Frame.svg">
								</image>
								<image class="gong" v-else src="/static/images/home/xu.svg"></image>
								<view class="f26bc0">{{item.product_name}}</view>
							</view>
							<view class="f22c9">{{item.product_subtitle}}</view>
						</view>
						<view class="btn f20bcf fcc">
							<view>我要对接</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view v-if="isLoadingMore" class="loading-more fcc">
				<up-loading-icon text='加载中...'></up-loading-icon>
			</view>
			<view v-if="noMoreData" class="no-more fcc">
				没有更多数据了
			</view>
		</view>
	</view>
</template>

<script setup>
	import CustomNavbar from '@/components/custom-navbar.vue'
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		mySupplyDemand
	} from '@/api/user'
	import {
		getClassification
	} from '@/api/login'

	// 系统信息
	const systemInfo = uni.getSystemInfoSync()
	const navBarHeight = computed(() => systemInfo.statusBarHeight + 44)

	// 数据状态
	const isLoading = ref(false)
	const isLoadingMore = ref(false)
	const noMoreData = ref(false)
	const isEmpty = ref(false)

	// 分页参数
	const page = ref(1)
	const limit = ref(10)
	const total = ref(0)

	// 供需类型
	const supplyDemandTypes = ref([{
			label: '全部',
			value: ''
		},
		{
			label: '供应',
			value: 19
		},
		{
			label: '需求',
			value: 20
		}
	])

	// 当前选中的供需类型
	const supply_demand = ref('')

	// 供需列表数据
	const supplyDemandList = ref([])

	// 初始化加载
	onMounted(() => {
		loadSupplyDemandList(true)
	})

	// 加载供需列表
	const loadSupplyDemandList = async (isRefresh = false) => {
		if (isLoading.value && !isRefresh) return

		try {
			if (isRefresh) {
				page.value = 1
				noMoreData.value = false
				isLoading.value = true
			} else {
				isLoadingMore.value = true
			}

			const params = {
				page: page.value,
				limit: limit.value,
				supply_demand: supply_demand.value
			}

			const res = await mySupplyDemand(params)

			if (res.code === 200) {
				if (isRefresh) {
					supplyDemandList.value = res.data.data
					total.value = res.data.total
				} else {
					supplyDemandList.value = [...supplyDemandList.value, ...res.data.data]
				}

				// 检查是否还有更多数据
				if (res.data.data.length < limit.value) {
					noMoreData.value = true
				}

				// 检查空状态
				isEmpty.value = supplyDemandList.value.length === 0
			}
		} catch (error) {
			console.error('加载供需列表失败:', error)
			uni.showToast({
				title: '加载失败，请重试',
				icon: 'none'
			})
		} finally {
			isLoading.value = false
			isLoadingMore.value = false
		}
	}

	// 切换供需类型
	const switchSupplyDemandType = (type) => {
		if (supply_demand.value === type) return
		supply_demand.value = type
		loadSupplyDemandList(true)
	}

	// 重置搜索
	const resetSearch = () => {
		supply_demand.value = ''
		loadSupplyDemandList(true)
	}

	// 加载更多
	onReachBottom(() => {
		if (isLoadingMore.value || noMoreData.value) return
		page.value += 1
		loadSupplyDemandList()
	})

	// 返回
	const handleBack = () => {
		uni.navigateBack()
	}

	// 查看详情
	const handleDetails = (item) => {
		uni.navigateTo({
			url: `/pages/supply/details?sd_id=${item.sd_id}`
		})
	}
</script>

<style lang="scss" scoped>
	.home {
		width: 100%;
		min-height: 100vh;
		background-color: #f7f7f7;

		.content {
			padding-top: var(--status-bar-height);
			padding-top: calc(var(--status-bar-height) + 70rpx);

			.search {
				background: #FFFFFF;
				padding: 16rpx 30rpx 22rpx 30rpx;
			}

			.empty-state {
				height: 60vh;
				padding: 40rpx;
				text-align: center;

				.empty-image {
					width: 300rpx;
					height: 300rpx;
					margin-bottom: 30rpx;
				}

				.empty-text {
					font-size: 28rpx;
					color: #999;
				}
			}

			.supplyList {
				.item {
					width: 334rpx;
					background: #FFFFFF;
					border-radius: 20rpx;
					overflow: hidden;

					.img {
						width: 334rpx;
						height: 300rpx;
					}

					.contentBox {
						padding: 24rpx 20rpx;

						.gong {
							width: 30rpx;
							height: 30rpx;
							margin-right: 10rpx;
						}

						.f26bc0 {
							font-size: 26rpx;
							font-weight: bold;
							color: #333;
						}

						.f22c9 {
							font-size: 22rpx;
							color: #999;
							margin-top: 10rpx;
						}

						.btn {
							width: 100%;
							height: 50rpx;
							background: linear-gradient(90deg, #398DFB 0%, #61DBFF 100%);
							border-radius: 10rpx;
							color: #FFFFFF;
							margin-top: 20rpx;
						}
					}
				}
			}

			.loading-more,
			.no-more {
				padding: 30rpx 0;
				color: #999;
				font-size: 26rpx;
			}
		}
	}

	.flex {
		display: flex;
	}

	.f30bc3 {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
	}

	.c398DFB {
		color: #398DFB;
	}

	.fcc {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.fcols {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.frw {
		display: flex;
		flex-wrap: wrap;
	}

	.p0030 {
		padding: 0 30rpx;
	}

	.mb37 {
		margin-bottom: 37rpx;
	}
</style>